[#ftl]
CREATE TABLE ${table.tableCode}(
[#list table.allcols as col]
  ${col.columnCode} ${col.columnDatatype}${setAttrFunc(col)}[#if col_has_next],[/#if]
[/#list]
[@setKeyFunc /]
)[@setTableComment2Func /];
[@setTableCommentFunc /]
[@setComment2Func /]

[#-- 设置字段属性 --]
[#function setAttrFunc col]
[#return setIncrementFunc(col)+setDefaultFunc(col)+setNullFunc(col)+setCommentFunc(col) ]
[/#function]
[#-- 设置字段自增 --]
[#function setIncrementFunc col]
[#if col.columnAutoIncrement == '1' && databaseType == 'mysql']
[#return " auto_increment" ]
[#elseif col.columnAutoIncrement == '1' && databaseType == 'sqlserver']
[#return " identity(1,1)" ]
[/#if]
[#return "" ]
[/#function]
[#-- 设置字段默认值--]
[#function setDefaultFunc col]
[#if col.columnDefault?has_content][#return " DEFAULT "+col.columnDefault ][/#if]
[#return "" ]
[/#function]
[#-- 设置字段是否可空 --]
[#function setNullFunc col]
[#if col.columnMandatory == '0'][#return " NOT NULL"][/#if]
[#return "" ]
[/#function]
[#-- 设置字段注释--]
[#function setCommentFunc col]
[#if colCommentType == '1']
[#if col.columnComment?has_content] COMMENT '${col.columnComment}'[/#if]
[/#if]
[#return "" ]
[/#function]
[#-- 设置字段主键--]
[#macro setKeyFunc]
[#if table.pkColumn?? && table.pkColumn?size > 0]
[#if keyType == '2']
  ,CONSTRAINT pk_${table.tableCode} PRIMARY KEY ([#list table.pkColumn as pk]${pk.columnCode}[#if pk_has_next],[/#if][/#list])
[#else]
  ,PRIMARY KEY ([#list table.pkColumn as pk]${pk.columnCode}[#if pk_has_next],[/#if][/#list])
[/#if]
[/#if]
[/#macro]
[#-- 设置字段注释2--]
[#macro setComment2Func ]
[#if databaseType == 'sqlserver']
[#list table.allcols as col]
[#if col.columnComment?has_content]
 EXEC sp_addextendedproperty
    'MS_Description', '${col.columnComment}',
    'SCHEMA', '${table.currentSchema!'dbo'}', 'TABLE', '${table.tableCode}', 'COLUMN', '${col.columnCode}';
[/#if]
[/#list]
[#elseif colCommentType == '2']
[#list table.allcols as col]
[#if col.columnComment?has_content] COMMENT ON COLUMN ${table.tableCode}.${col.columnCode} IS '${col.columnComment}';[/#if]
[/#list]
[/#if]
[/#macro]
[#-- 设置表注释--]
[#macro setTableCommentFunc ]
[#if table.tableComment?has_content]
[#if databaseType == 'sqlserver']
 EXEC sp_addextendedproperty
   'MS_Description', '${table.tableComment}',
   'SCHEMA', '${table.currentSchema!'dbo'}', 'TABLE', '${table.tableCode}';
[#elseif tblCommentType == '2']
 COMMENT ON TABLE ${table.tableCode} IS '${table.tableComment}';
[/#if]
[/#if]
[/#macro]
[#-- 设置表注释2--]
[#macro setTableComment2Func ]
[#if table.tableComment?has_content]
[#if tblCommentType == '1']
 COMMENT '${table.tableComment}';
[/#if]
[/#if]
[/#macro]